package com.toycloud.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.toycloud.business.domain.DemandLog;

import com.toycloud.business.domain.bo.DemandLogBo;
import com.toycloud.business.service.ISelectJsonService;
import com.toycloud.common.core.utils.DateUtils;
import com.toycloud.common.utils.StringUtils;
import com.toycloud.system.domain.selectJson.SelectJsonReocrd;
import com.toycloud.system.mapper.selectJson.SelectJsonRecordMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Map;

/**
 * demandLogService业务层处理
 *
 * @author binlu6
 * @date 2024-11-06
 */
@RequiredArgsConstructor
@Service
public class SelectJsonServiceImpl implements ISelectJsonService {

    private final SelectJsonRecordMapper baseMapper;



    /**
     * 查询demandLog
     */
    @Override
    public String queryById(String id){
        SelectJsonReocrd selectJsonReocrd =baseMapper.selectById(id);
        String json = "缓存信息失效";
        if(selectJsonReocrd!=null){
            json = selectJsonReocrd.getSelectJson();
        }
        return json;
    }



    /**
     * 新增demandLog
     */
    @Override
    public String insertByBo(SelectJsonReocrd selectJsonReocrd) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        selectJsonReocrd.setCreateTime(LocalDateTime.parse(DateUtils.getTime(), formatter));
        boolean flag = baseMapper.insert(selectJsonReocrd) > 0;
        if (flag) {
            return(selectJsonReocrd.getId());
        }else{
            return "";
        }
    }


    /**
     * 修改demandLog
     */
    @Override
    public Boolean updateByBo(SelectJsonReocrd selectJsonReocrd) {
        return baseMapper.updateById(selectJsonReocrd) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DemandLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除demandLog
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Scheduled(cron = "0 0 0 1 * ?")
    public void deleteRecord(){
        LocalDateTime oneMonthAgo = LocalDateTime.now()
                .minus(1, ChronoUnit.MONTHS); // 减去整整一个月
        SelectJsonReocrd bo = new SelectJsonReocrd();
        bo.setCreateTime(oneMonthAgo);
        LambdaQueryWrapper<SelectJsonReocrd> lqw = buildQueryWrapper(bo);
        baseMapper.delete(lqw);
    }

    private LambdaQueryWrapper<SelectJsonReocrd> buildQueryWrapper(SelectJsonReocrd bo) {
        LambdaQueryWrapper<SelectJsonReocrd> lqw = Wrappers.lambdaQuery();
        // 从 params 中提取 createTime 条件
        if (bo.getCreateTime()!=null) {
                lqw.lt(SelectJsonReocrd::getCreateTime, bo.getCreateTime()); // 添加 "create_time < createTime" 条件
        }
        return lqw;
    }



}
